<template>
  <a-modal
    :title="title"
    :width="800"
    :ok=false
    :visible="visible"
    :confirmLoading="confirmLoading"
    :okButtonProps="{ props: {disabled: disableSubmit} }"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="所属区划">
          <a-tree-select
            style="width: 100%"
            v-model="districtCode"
            :tree-data="districtTreeData"
            :tree-checkable='false'
            :treeExpandedKeys="expandedKeys"
            v-decorator="['districtCode',validatorRules.districtCode]"
            @treeExpand='treeExpand'
            @select="onSelect"
          />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="行业类别">
          <a-tree-select
            style="width: 100%"
            :tree-data="industryTreeData"
            :tree-checkable='false'
            v-decorator="['industryId',validatorRules.industry]"
            @select="onIndustrySelect"
          />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import { queryIdTree, listDepartRoles,queryDistrictTreeList,queryIndustryTreeList } from '@/api/api'
  import ATextarea from 'ant-design-vue/es/input/TextArea'
  import JTreeSelectDistrict from '@/components/ksourcebiz/JTreeSelectDistrict'

  export default {
    name: "SysDepartModal",
    components: { ATextarea ,JTreeSelectDistrict},
    data () {
      return {
        departTree:[],
        orgTypeData:[],
        districtTreeData:[],
        industryTreeData:[],
        expandedKeys:[],
        phoneWarning:'',
        departName:"",
        districtCode:"",
        title:"操作",
        seen:false,
        visible: false,
        condition:true,
        disableSubmit:false,
        model: {},
        roleList:[],
        selectedRoles:[],
        menuhidden:false,
        menuusing:true,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          districtCode:{rules: [{ required: true, message: '请选择区划!' }]},
          industry:{rules: [{ required: true, message: '请选择行业类型!' }]}
        },
        url: {
          init: "/platform/system/sysDepart/init",
        },
        dictDisabled:true,
      }
    },
    created () {
      this.loadDistrictData();
      this.loadIndustryData();
    },
    methods: {
      onSelect(key){
        this.districtCode=key
      },
      onIndustrySelect(key){
        this.industryId=key
      },
      loadTreeData(){
        var that = this;
        queryIdTree().then((res)=>{
          if(res.success){
            that.departTree = [];
            for (let i = 0; i < res.result.length; i++) {
              let temp = res.result[i];
              that.departTree.push(temp);
            }
          }

        })
      },
      loadIndustryData(){
        queryIndustryTreeList().then(res=>{
          if(res.success){
            let arr = [...res.result]
            this.initIndustryTreeData(arr)
            this.industryTreeData = arr
          }
        })
      },
      initIndustryTreeData(res){
        for(let r of res){
          r.title=r.name
          r.leaf=true
          r.key=r.id
          r.value=r.id
        }
      },
      loadDepartRoles(departId){
        listDepartRoles({departId:departId}).then((res)=>{
          if(res.success){
            this.selectedRoles = res.result;
          }else{
            console.log(res.message);
          }
        });
      },
      loadDistrictData(){
        queryDistrictTreeList().then(res=>{
          if(res.success){
            let arr = [...res.result]
            this.districtTreeData = arr
            this.initDistrictExpandedKeys(res.result)
          }
        })
      },
      initDistrictExpandedKeys(arr){
        if(arr && arr.length>0){
          let keys = []
          for(let item of arr){
            if(item.children && item.children.length>0){
              keys.push(item.id)
            }
          }
          this.expandedKeys=[...keys]
        }else{
          this.expandedKeys=[]
        }
      },
      treeExpand(keys){
        this.expandedKeys=keys
      },
      add () {
        this.seen = true;
        this.dictDisabled = true;
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.disableSubmit = false;
        this.visible = false;
        this.selectedRoles = [];
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            formData.selectedRoles = this.selectedRoles.length>0?this.selectedRoles.join(","):'';
            //时间格式化
            console.log(formData)
            httpAction(this.url.init,formData,"post").then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.loadTreeData();
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })

          }
        })
      },
      handleCancel () {
        this.close()
      }
    }
  }
</script>

<style scoped>

</style>